package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Descroption: 发布帖子功能
 * User:dell
 * Date:2025-09-08
 * Time:9:09
 */

public class PostPage extends Utils {
    /**
     * 发布帖子功能
     */
    public static String url = "http://81.70.248.108:58080/index.html";
    public PostPage() {
        super(url);
    }

    /**
     * 检查页面元素
     */
    public void checkPost() throws InterruptedException {
        webDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/a[1]")).click();
        Thread.sleep(3000);
        //1.获取操作元素
        By title = By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2");
        By boardTitle = By.xpath("//*[@id=\"article_post_borad\"]");
        By articleTitle = By.xpath("//*[@id=\"article_post_title\"]");
        By btn = By.xpath("//*[@id=\"article_post_submit\"]");

        String titleText = webDriver.findElement(title).getText();
        String btnText = webDriver.findElement(btn).getText();
        System.out.println(btnText);
        webDriver.findElement(boardTitle);
        webDriver.findElement(articleTitle);

        assert titleText.equals("发新帖"):"功能板块不一致";
        assert !btnText.isEmpty();
    }
    /**
     * 发布帖子成功
     */
    public void postSuc() throws InterruptedException {
        //1.获取操作元素
        By Title = By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2");
        By boardTitle = By.xpath("//*[@id=\"article_post_borad\"]");
        By articleTitle = By.xpath("//*[@id=\"article_post_title\"]");
        By btn = By.xpath("//*[@id=\"article_post_submit\"]");

        //2.获取发布板块信息
        //2.1 获取下拉框定位元素
        WebElement boardSelect = webDriver.findElement(boardTitle);
        Select select = new Select(boardSelect);
        //2.2 获取所有选项
        List<WebElement> options = select.getOptions();
        //2.2生成随机索引值
        Random random = new Random();
        int randomIndex = random.nextInt(options.size());
        String boardText = options.get(randomIndex).getText();
        String boardValue = options.get(randomIndex).getAttribute("value");

        select.selectByIndex(randomIndex);
        System.out.println("选择的板块："+boardText+"   value:"+boardValue);
        //2.3 查看是否验证成功
        String actualBoard = select.getFirstSelectedOption().getText();
        assert actualBoard.equals(boardText):"选择失败";

        //3 找到标题区域，输入标题（动态生成）
        webDriver.findElement(articleTitle).clear();
        SimpleDateFormat sim = new SimpleDateFormat("HHmmssSS");
        String titleTime = sim.format(System.currentTimeMillis());
        String titleBefore = "自动创建文章标题"+titleTime;
        webDriver.findElement(articleTitle).sendKeys(titleBefore);


        //4 使用鼠标操作，在类容区操作
        WebElement ele = webDriver.findElement(By.xpath("//*[@id=\"edit-article\"]/div[1]/div[6]/div[1]/div/div"));
        new Actions(webDriver).doubleClick(ele).sendKeys(Keys.DELETE).perform();
        //延迟1秒
        Thread.sleep(1000);
        new Actions(webDriver).doubleClick(ele).sendKeys("自动话测试类容").perform();
        //等待发布标签可以点击
        //延迟一秒
        Thread.sleep(1000);
        WebDriverWait btnWait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        btnWait.until(ExpectedConditions.elementToBeClickable(btn));
        WebElement btnEle = webDriver.findElement(btn);
        new Actions(webDriver).click(btnEle).perform();

        //5 发布成功后获取第一篇文章，查看文章标题是否一致
        //*[@id="artical-items-body"]/div[1]/div/div[2]/div[1]

        String titleAfter = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).getText();
        assert titleAfter.equals(titleBefore):"文章标题不一致";
    }

    /**
     * 发布失败
     */
    public void postFail() throws InterruptedException {
        //1.获取操作元素
        By Title = By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2");
        By boardTitle = By.xpath("//*[@id=\"article_post_borad\"]");
        By articleTitle = By.xpath("//*[@id=\"article_post_title\"]");
        By btn = By.xpath("//*[@id=\"article_post_submit\"]");


        //2.标题类容都为空，直接点击发布
        WebDriverWait btnWait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        btnWait.until(ExpectedConditions.elementToBeClickable(btn));
        WebElement btnEle = webDriver.findElement(btn);
        new Actions(webDriver).click(btnEle).perform();


        //3. 标题不为空
        // 找到标题区域，输入标题（动态生成）
        SimpleDateFormat sim = new SimpleDateFormat("HHmmssSS");
        String titleTime = sim.format(System.currentTimeMillis());
        String titleBefore = "自动创建文章标题"+titleTime;
        webDriver.findElement(articleTitle).sendKeys(titleBefore);

        WebDriverWait btnWait1 = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        btnWait1.until(ExpectedConditions.elementToBeClickable(btn));
        WebElement btnEle1 = webDriver.findElement(btn);
        new Actions(webDriver).click(btnEle1).perform();

        //4. 类容为空
        //清空标题
        webDriver.findElement(articleTitle).clear();

        //使用鼠标操作，在类容区操作
        WebElement ele = webDriver.findElement(By.xpath("//*[@id=\"edit-article\"]/div[1]/div[6]/div[1]/div/div"));
        new Actions(webDriver).doubleClick(ele).sendKeys("自动话测试类容").perform();
        //等待发布标签可以点击
        //延迟一秒
        Thread.sleep(1000);
        WebDriverWait btnWait2 = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        btnWait2.until(ExpectedConditions.elementToBeClickable(btn));
        WebElement btnEle2 = webDriver.findElement(btn);
        new Actions(webDriver).click(btnEle2).perform();
    }

}
